The Tidyverse

* __  _    __   .    o           *  . 
 / /_(_)__/ /_ ___  _____ _______ ___ 
/ __/ / _  / // / |/ / -_) __(_-</ -_)
\__/_/\_,_/\_, /|___/\__/_/ /___/\__/ 
     *  . /___/      o      .       * 

We are going to be working around in the tidyverse for a good chunk of our time together. The whole point of the tidyverse is to offer a grammer of verbs. It is going to help us in a lot of the situations that we are going to be seeing.

Another great feature of the tidyverse is the pipe: %>%

It does the same thing as the Unix |, but | in R is an or operator.

With all of the glowing praise for the tidyverse, we are still going to see some base R. Sometimes, it will demonstrate great reasons for using the tidyverse. In other situations, it will help you to not be afraid to use it when situations arise.

Some Demonstrations

Summary Tables

library(ggplot2)

plotDat = aggregate(diamonds$cut, by = list(cut = diamonds$cut), FUN = length)

colnames(plotDat)[2] = "n"

plotDat
        cut     n
1      Fair  1610
2      Good  4906
3 Very Good 12082
4   Premium 13791
5     Ideal 21551

Visual

ggplot(plotDat, aes(x = cut, y = n)) +
  geom_point(aes(size = n)) +
  theme_minimal()

(Im)Proper Plotting

Look at help(mtcars) and check out the variables. Can you spot what is wrong with this plot?

ggplot(mtcars, aes(x = wt, y = mpg, color = am)) + 
  geom_point() +
  theme_minimal()

Proper Plotting

The plot below is likely better.

library(dplyr)

mtcars$amFactor = as.factor(mtcars$am) 

ggplot(mtcars, aes(x = wt, y = mpg, color = amFactor)) + 
  geom_point() +
  theme_minimal()

Pipes: Making Life Easier

Recall some of the things that we just saw:

plotDat = aggregate(diamonds$cut, by = list(cut = diamonds$cut), FUN = length)

colnames(plotDat)[2] = "n"

ggplot(plotDat, aes(x = cut, y = n)) +
  geom_point(aes(size = n)) +
  theme_minimal()

This is somewhat tricky code. We have to create a new object with the oft-muddy aggregate and reset a column name (by magic number in an index, no less).

This can be made much easier with dplyr:

diamonds %>% 
  group_by(cut) %>% 
  summarize(n = n()) %>% 
  ggplot(., aes(x = cut, y = n)) +
  geom_point(aes(size = n)) +
  theme_minimal()

It isn’t a reduction in lines, but it is certainly clearer and follows a more logical thought process. This is the whole point of the tidyverse (and dplyr specifically) – allowing you to write how you would explain the process.

As an added bonus, we don’t need to create a bunch of different objects to do something simple.

We can see that dplyr will also make the plot for am easier.

mtcars %>% 
  mutate(am = as.factor(am)) %>%  
  ggplot(., aes(x = wt, y = mpg, color = am)) + 
  geom_point() +
  theme_minimal()

On Code Golf

You will often notice that a dplyr chunk might take a few more lines to work through than base R alone – don’t consider this as a bad thing. There will be many times in this course and in your potential work that you might think that you need to use as few lines as possible. Resist this temptation. Sometime you need to break something up into many lines and create new objects – this ability is exactly why we use R!

Data Import

Importing data is often the easiest part (never too hard to import a nice .csv). Sometimes, though, we need some other strategies.

Delimited Files

Frequently, you will see nicely delimited text files that are not .csv files – these are often tab-delimited file, but they can take other forms.

read.table("https://download.bls.gov/pub/time.series/ce/ce.data.42a.RetailTrade.Employment", 
           header = TRUE, sep = "\t")

Is the same as:

read.delim("https://download.bls.gov/pub/time.series/ce/ce.data.42a.RetailTrade.Employment")

The read.table() function gives you added flexibility to specify many different parameters.

Examine the following file from SDC Platinum and read it in properly:

SDC Wackiness

How did you do?

Did you notice anything about these files? They are not really very big, but they might have taken a little bit of time to read in. There have been times where people have commented that R is too slow on the read side. If you find you files are not being read quickly enough, you can try a few alternatives: readr data.table

Try the following:

readrTest = readr::read_delim("https://download.bls.gov/pub/time.series/ce/ce.data.42a.RetailTrade.Employment", 
                              delim = "\t")
dtTest = data.table::fread("https://download.bls.gov/pub/time.series/ce/ce.data.42a.RetailTrade.Employment", 
                           sep = "\t")

That SDC file that might have taken a few minutes will now take just a few seconds:

sdc = readr::read_delim("https://www3.nd.edu/~sberry5/data/sdcTest.txt", 
                        delim = "^")

Pretty awesome, right?

At times, you will get data in some proprietary format. That is when you need to turn to other places.

Excel

readxl::read_excel(path = "folder/fileName")

What do we know about Excel workbooks? Check out the help on readxl and let me know our path forward.

SAS

haven::read_sas(data_file = "https://www3.nd.edu/~sberry5/data/wciklink_gvkey.sas7bdat")

Stata

haven::read_dta(file = "https://www3.nd.edu/~sberry5/data/stataExample.dta")

SPSS

We often see the -99 added as the missing value in SPSS (of course, there is no way that -99 would ever be an actual value, right?).

haven::read_spss(file = "https://www3.nd.edu/~sberry5/data/spssExample.sav", 
                 user_na = "-99")

HTML

Depending on your needs, reading an html table into R is getting to be too easy.

library(rvest)

cpi = read_html("http://www.usinflationcalculator.com/inflation/consumer-price-index-and-annual-percent-changes-from-1913-to-2008/") %>% 
  html_table(fill = TRUE)

Things might get a bit tricky:

highest = read_html("https://en.wikipedia.org/wiki/List_of_highest-grossing_films") %>% 
  html_table(fill = TRUE)

What is the return of this call?

rio

For many of these tasks, you can just use the rio package – you give it the file and it will do the rest!

rio::import("folder/file")

Nested Structures

JSON

Web-based graphics started getting popular not too long ago. Generally, stats people were not using them, but web developer-type folks were. They needed a structure that would work well for the web and interact with their JavaScript-based graphics – thus, JavaScript Object Notation (JSON) was born. You will see JSON come out of many web-based interfaces.

There are a few JSON-reading packages in R, but jsonlite tends to work pretty well.

###optionsDataBrief

jsonTest = jsonlite::read_json(path = "https://www3.nd.edu/~sberry5/data/optionsDataBrief.json", 
                                simplifyVector = TRUE)

This is a very simple form of JSON. We are going to see a hairier verson of this data soon.

JSON Dangers

There is JSON and then there is JSON. You might find yourself some interesting data and want to bring it in, but an error happens and you have no idea why the read_json function is telling you that the file is not JSON.

Not all JSON is pure JSON! When that is the case, you will need to create pure JSON.

XML

It is getting to be a bit less common (still prevalent), but XML (eXtensible Markup Language) is another type of nested structure common on the web.

xml2::read_xml()

Mass Reading

Everything we just learned is great and you will use them all in your data wrangling missions.

Fortunately (or unfortunately, depending on how you look at it), it is not the whole story – you will frequently be reading in many files of the same time.

If you have two files, you might be able to get away with brute force:

# DO NOT RUN:

myData1 = read.csv("test.csv")

myData2 = read.csv("test2.csv")

Would you want to do this for 5 files? What about 100? Or 1000? I will answer it for you: no!

The chunks below introduce some very important functions. We are going to see lapply again – it is important that you learn to love the apply family!

# DO NOT RUN:

allFiles = list.files(path = "", all.files = TRUE, full.names = TRUE, 
                      recursive = TRUE, include.dirs = FALSE)

allFilesRead = lapply(allFiles, function(x) read.csv(x, stringsAsFactors = FALSE))

allData = do.call("rbind", allFilesRead)

You can also use rio:

# DO NOT RUN:

rio::import_list("", rbind = TRUE)

Your Turn

  1. Take a look at this file:
  2. Determine how it should be read in.
  3. Summarize the data.
  4. Visualize the data in any way you wish!

The Grammar Of Data

One of the major aims of the tidyverse is to provide a clear and consistent grammar to data manipulation. This is helpful when diving deeper into the weeds.

Do you remember this?

highest = read_html("https://en.wikipedia.org/wiki/List_of_highest-grossing_films") %>% 
  html_table(fill = TRUE)

What did we get out of this? It was a big list of data frames. If we are looking for only one thing and we know that it is the first thing, we have some options:

highest = highest[[1]]

This is great for keeping the object at first and then plucking out what we want. If you want the whole thing to be together, though, we have even more options:

highest = read_html("https://en.wikipedia.org/wiki/List_of_highest-grossing_films") %>% 
  html_table(fill = TRUE) %>% 
  `[[`(1)

And now we see why R mystifies people. What does is that bit of nonsense at the end. It is really just an index shortcut. Once you know how to use it, it is great; however, it will make you shake your head if you see it in the wild without knowing about it first.

This is where the benefit of tidyverse becomes clear.

highest = read_html("https://en.wikipedia.org/wiki/List_of_highest-grossing_films") %>% 
  html_table(fill = TRUE) %>%
  magrittr::extract2(1)

Or…

highest = read_html("https://en.wikipedia.org/wiki/List_of_highest-grossing_films") %>% 
  html_table(fill = TRUE) %>%
  purrr::pluck(1)

Do be careful, though, because we can have some issues in function masking and pluck from purrr does something very different than pluck from dplyr.

Someone try it and tell me what happens!

Selecting

Base

There are many ways to select variables with base R:

mtcars[, c(1:5, 7:8)]

keepers = c("mpg", "cyl", "disp", "hp", "drat", "qsec", "vs")

mtcars[, keepers]

mtcars[, c("mpg", grep("^c", names(mtcars), values = TRUE))]

You can also drop variables:

mtcars[, -c(1:2)]
                     disp  hp drat    wt  qsec vs am gear carb amFactor
Mazda RX4           160.0 110 3.90 2.620 16.46  0  1    4    4        1
Mazda RX4 Wag       160.0 110 3.90 2.875 17.02  0  1    4    4        1
Datsun 710          108.0  93 3.85 2.320 18.61  1  1    4    1        1
Hornet 4 Drive      258.0 110 3.08 3.215 19.44  1  0    3    1        0
Hornet Sportabout   360.0 175 3.15 3.440 17.02  0  0    3    2        0
Valiant             225.0 105 2.76 3.460 20.22  1  0    3    1        0
Duster 360          360.0 245 3.21 3.570 15.84  0  0    3    4        0
Merc 240D           146.7  62 3.69 3.190 20.00  1  0    4    2        0
Merc 230            140.8  95 3.92 3.150 22.90  1  0    4    2        0
Merc 280            167.6 123 3.92 3.440 18.30  1  0    4    4        0
Merc 280C           167.6 123 3.92 3.440 18.90  1  0    4    4        0
Merc 450SE          275.8 180 3.07 4.070 17.40  0  0    3    3        0
Merc 450SL          275.8 180 3.07 3.730 17.60  0  0    3    3        0
Merc 450SLC         275.8 180 3.07 3.780 18.00  0  0    3    3        0
Cadillac Fleetwood  472.0 205 2.93 5.250 17.98  0  0    3    4        0
Lincoln Continental 460.0 215 3.00 5.424 17.82  0  0    3    4        0
Chrysler Imperial   440.0 230 3.23 5.345 17.42  0  0    3    4        0
Fiat 128             78.7  66 4.08 2.200 19.47  1  1    4    1        1
Honda Civic          75.7  52 4.93 1.615 18.52  1  1    4    2        1
Toyota Corolla       71.1  65 4.22 1.835 19.90  1  1    4    1        1
Toyota Corona       120.1  97 3.70 2.465 20.01  1  0    3    1        0
Dodge Challenger    318.0 150 2.76 3.520 16.87  0  0    3    2        0
AMC Javelin         304.0 150 3.15 3.435 17.30  0  0    3    2        0
Camaro Z28          350.0 245 3.73 3.840 15.41  0  0    3    4        0
Pontiac Firebird    400.0 175 3.08 3.845 17.05  0  0    3    2        0
Fiat X1-9            79.0  66 4.08 1.935 18.90  1  1    4    1        1
Porsche 914-2       120.3  91 4.43 2.140 16.70  0  1    5    2        1
Lotus Europa         95.1 113 3.77 1.513 16.90  1  1    5    2        1
Ford Pantera L      351.0 264 4.22 3.170 14.50  0  1    5    4        1
Ferrari Dino        145.0 175 3.62 2.770 15.50  0  1    5    6        1
Maserati Bora       301.0 335 3.54 3.570 14.60  0  1    5    8        1
Volvo 142E          121.0 109 4.11 2.780 18.60  1  1    4    2        1
dropVars = c("vs", "drat")

mtcars[, !(names(mtcars) %in% dropVars)]
                     mpg cyl  disp  hp    wt  qsec am gear carb amFactor
Mazda RX4           21.0   6 160.0 110 2.620 16.46  1    4    4        1
Mazda RX4 Wag       21.0   6 160.0 110 2.875 17.02  1    4    4        1
Datsun 710          22.8   4 108.0  93 2.320 18.61  1    4    1        1
Hornet 4 Drive      21.4   6 258.0 110 3.215 19.44  0    3    1        0
Hornet Sportabout   18.7   8 360.0 175 3.440 17.02  0    3    2        0
Valiant             18.1   6 225.0 105 3.460 20.22  0    3    1        0
Duster 360          14.3   8 360.0 245 3.570 15.84  0    3    4        0
Merc 240D           24.4   4 146.7  62 3.190 20.00  0    4    2        0
Merc 230            22.8   4 140.8  95 3.150 22.90  0    4    2        0
Merc 280            19.2   6 167.6 123 3.440 18.30  0    4    4        0
Merc 280C           17.8   6 167.6 123 3.440 18.90  0    4    4        0
Merc 450SE          16.4   8 275.8 180 4.070 17.40  0    3    3        0
Merc 450SL          17.3   8 275.8 180 3.730 17.60  0    3    3        0
Merc 450SLC         15.2   8 275.8 180 3.780 18.00  0    3    3        0
Cadillac Fleetwood  10.4   8 472.0 205 5.250 17.98  0    3    4        0
Lincoln Continental 10.4   8 460.0 215 5.424 17.82  0    3    4        0
Chrysler Imperial   14.7   8 440.0 230 5.345 17.42  0    3    4        0
Fiat 128            32.4   4  78.7  66 2.200 19.47  1    4    1        1
Honda Civic         30.4   4  75.7  52 1.615 18.52  1    4    2        1
Toyota Corolla      33.9   4  71.1  65 1.835 19.90  1    4    1        1
Toyota Corona       21.5   4 120.1  97 2.465 20.01  0    3    1        0
Dodge Challenger    15.5   8 318.0 150 3.520 16.87  0    3    2        0
AMC Javelin         15.2   8 304.0 150 3.435 17.30  0    3    2        0
Camaro Z28          13.3   8 350.0 245 3.840 15.41  0    3    4        0
Pontiac Firebird    19.2   8 400.0 175 3.845 17.05  0    3    2        0
Fiat X1-9           27.3   4  79.0  66 1.935 18.90  1    4    1        1
Porsche 914-2       26.0   4 120.3  91 2.140 16.70  1    5    2        1
Lotus Europa        30.4   4  95.1 113 1.513 16.90  1    5    2        1
Ford Pantera L      15.8   8 351.0 264 3.170 14.50  1    5    4        1
Ferrari Dino        19.7   6 145.0 175 2.770 15.50  1    5    6        1
Maserati Bora       15.0   8 301.0 335 3.570 14.60  1    5    8        1
Volvo 142E          21.4   4 121.0 109 2.780 18.60  1    4    2        1

Issues?

For starters, the magic numbers are a no-go. The keepers lines could work, but would be a pain if we had a lot of variables.

Let’s check this wacky stuff out where we want all variables that start with “age” and variables that likely represent questions (x1, x2, x3, …):

library(lavaan)

testData = HolzingerSwineford1939

names(testData)
 [1] "id"     "sex"    "ageyr"  "agemo"  "school" "grade"  "x1"    
 [8] "x2"     "x3"     "x4"     "x5"     "x6"     "x7"     "x8"    
[15] "x9"    
keepers = c(grep("^age", names(testData), value = TRUE), 
            paste("x", 1:9, sep = ""))

testData = testData[, keepers]

Not only do we have another regular expression, but we also have this paste line to create variable names. It seems like too much work to do something simple!

While not beautiful, these are perfectly valid ways to do this work. I have such sights to show you, but don’t forget about this stuff – you never know when you might need to use it.

dplyr

We have already seen a bit of dplyr, but we are going to dive right into some of the functions now.

In base R, we have to do some chanting to select our variables. With dplyr, we can just use select:

mtcars %>% 
  select(mpg, cyl, am)
                     mpg cyl am
Mazda RX4           21.0   6  1
Mazda RX4 Wag       21.0   6  1
Datsun 710          22.8   4  1
Hornet 4 Drive      21.4   6  0
Hornet Sportabout   18.7   8  0
Valiant             18.1   6  0
Duster 360          14.3   8  0
Merc 240D           24.4   4  0
Merc 230            22.8   4  0
Merc 280            19.2   6  0
Merc 280C           17.8   6  0
Merc 450SE          16.4   8  0
Merc 450SL          17.3   8  0
Merc 450SLC         15.2   8  0
Cadillac Fleetwood  10.4   8  0
Lincoln Continental 10.4   8  0
Chrysler Imperial   14.7   8  0
Fiat 128            32.4   4  1
Honda Civic         30.4   4  1
Toyota Corolla      33.9   4  1
Toyota Corona       21.5   4  0
Dodge Challenger    15.5   8  0
AMC Javelin         15.2   8  0
Camaro Z28          13.3   8  0
Pontiac Firebird    19.2   8  0
Fiat X1-9           27.3   4  1
Porsche 914-2       26.0   4  1
Lotus Europa        30.4   4  1
Ford Pantera L      15.8   8  1
Ferrari Dino        19.7   6  1
Maserati Bora       15.0   8  1
Volvo 142E          21.4   4  1

We can also drop variables with the -:

mtcars %>% 
  select(-vs)
                     mpg cyl  disp  hp drat    wt  qsec am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1    4    2
                    amFactor
Mazda RX4                  1
Mazda RX4 Wag              1
Datsun 710                 1
Hornet 4 Drive             0
Hornet Sportabout          0
Valiant                    0
Duster 360                 0
Merc 240D                  0
Merc 230                   0
Merc 280                   0
Merc 280C                  0
Merc 450SE                 0
Merc 450SL                 0
Merc 450SLC                0
Cadillac Fleetwood         0
Lincoln Continental        0
Chrysler Imperial          0
Fiat 128                   1
Honda Civic                1
Toyota Corolla             1
Toyota Corona              0
Dodge Challenger           0
AMC Javelin                0
Camaro Z28                 0
Pontiac Firebird           0
Fiat X1-9                  1
Porsche 914-2              1
Lotus Europa               1
Ford Pantera L             1
Ferrari Dino               1
Maserati Bora              1
Volvo 142E                 1

We also have several helper functions that we can use:

HolzingerSwineford1939 %>% 
  select(num_range("x", 1:9), starts_with("age"), 
         matches("^s.*.l$"))
           x1   x2    x3        x4   x5        x6       x7    x8       x9
1   3.3333333 7.75 0.375 2.3333333 5.75 1.2857143 3.391304  5.75 6.361111
2   5.3333333 5.25 2.125 1.6666667 3.00 1.2857143 3.782609  6.25 7.916667
3   4.5000000 5.25 1.875 1.0000000 1.75 0.4285714 3.260870  3.90 4.416667
4   5.3333333 7.75 3.000 2.6666667 4.50 2.4285714 3.000000  5.30 4.861111
5   4.8333333 4.75 0.875 2.6666667 4.00 2.5714286 3.695652  6.30 5.916667
6   5.3333333 5.00 2.250 1.0000000 3.00 0.8571429 4.347826  6.65 7.500000
7   2.8333333 6.00 1.000 3.3333333 6.00 2.8571429 4.695652  6.20 4.861111
8   5.6666667 6.25 1.875 3.6666667 4.25 1.2857143 3.391304  5.15 3.666667
9   4.5000000 5.75 1.500 2.6666667 5.75 2.7142857 4.521739  4.65 7.361111
10  3.5000000 5.25 0.750 2.6666667 5.00 2.5714286 4.130435  4.55 4.361111
11  3.6666667 5.75 2.000 2.0000000 3.50 1.5714286 3.739130  5.70 4.305556
12  5.8333333 6.00 2.875 2.6666667 4.50 2.7142857 3.695652  5.15 4.138889
13  5.6666667 4.50 4.125 2.6666667 4.00 2.2857143 5.869565  5.20 5.861111
14  6.0000000 5.50 1.750 4.6666667 4.00 1.5714286 5.130435  4.70 4.444444
15  5.8333333 5.75 3.625 5.0000000 5.50 3.0000000 4.000000  4.35 5.861111
16  4.6666667 4.75 2.375 2.6666667 4.25 0.7142857 4.086957  3.80 5.138889
17  4.3333333 4.75 1.500 2.0000000 4.00 1.2857143 3.695652  6.65 5.250000
18  5.0000000 6.75 2.250 2.0000000 2.50 1.7142857 4.000000  5.25 5.444444
19  5.6666667 5.25 4.000 4.3333333 5.25 3.7142857 3.913044  4.85 5.750000
20  6.3333333 8.75 3.000 3.6666667 3.75 2.5714286 3.478261  5.35 4.916667
21  5.8333333 8.00 2.000 1.6666667 2.50 0.5714286 2.608696  4.60 5.388889
22  6.6666667 8.50 4.125 2.0000000 3.25 2.1428571 4.478261  5.45 7.000000
23  5.0000000 6.25 1.875 3.3333333 5.75 3.1428571 3.478261  4.60 5.000000
24  3.8333333 5.50 1.625 2.6666667 3.00 1.0000000 5.826087  5.30 6.777778
25  5.6666667 5.50 1.250 2.3333333 3.75 1.2857143 4.695652  4.60 4.138889
26  5.3333333 4.00 3.375 1.6666667 3.50 1.4285714 5.739130  6.25 4.333333
27  5.5000000 5.25 4.500 2.6666667 2.25 0.7142857 4.130435  5.10 4.527778
28  6.0000000 5.00 2.125 1.6666667 3.00 1.1428571 2.826087  5.55 4.416667
29  4.6666667 6.00 4.250 2.0000000 3.00 2.0000000 5.130435  5.85 8.611111
30  5.0000000 4.50 0.750 2.6666667 3.25 1.8571429 4.652174  4.85 5.444444
31  3.5000000 5.75 1.375 2.0000000 3.50 1.8571429 4.826087  6.95 5.972222
32  3.0000000 6.00 0.250 1.6666667 3.00 1.0000000 2.043478  3.65 3.361111
33  5.0000000 5.25 1.750 2.6666667 5.25 2.0000000 2.695652  4.30 4.805556
34  4.1666667 6.00 2.375 3.3333333 4.25 1.8571429 5.391304  4.35 5.638889
35  3.3333333 3.75 1.500 1.3333333 3.00 0.8571429 2.782609  5.20 4.833333
36  4.8333333 5.25 0.500 1.6666667 4.75 1.4285714 3.130435  3.75 4.916667
37  5.5000000 7.00 3.500 2.6666667 4.00 2.0000000 3.826087  4.00 5.305556
38  3.8333333 4.50 2.250 3.0000000 3.00 1.7142857 4.086957  3.50 5.083333
39  6.3333333 4.00 3.875 4.0000000 5.25 3.2857143 5.521739  5.45 5.111111
40  5.8333333 7.75 2.500 3.0000000 5.25 3.1428571 3.217391  4.50 4.888889
41  3.8333333 5.75 1.625 2.6666667 4.25 2.1428571 4.913043  5.10 4.638889
42  3.1666667 5.00 1.250 1.6666667 4.25 1.5714286 2.826087  5.30 4.777778
43  1.8333333 5.25 1.000 1.6666667 3.75 0.5714286 3.956522  4.75 2.777778
44  4.1666667 5.25 1.875 2.0000000 3.00 1.4285714 3.956522  5.60 6.666667
45  6.3333333 5.50 2.750 4.6666667 5.50 2.0000000 5.391304  5.00 6.861111
46  6.0000000 5.50 4.500 3.0000000 4.25 1.2857143 4.826087  5.00 5.000000
47  7.1666667 8.50 4.000 0.6666667 1.00 1.0000000 3.347826  6.35 6.277778
48  3.1666667 4.75 1.375 2.6666667 3.25 1.2857143 6.130435  8.00 5.444444
49  4.3333333 5.50 2.750 2.0000000 2.25 1.0000000 5.652174  5.90 6.055556
50  4.5000000 6.25 1.125 3.6666667 5.50 2.1428571 2.565217  4.80 5.527778
51  5.5000000 5.75 3.750 1.6666667 2.25 0.1428571 2.347826  5.05 5.500000
52  7.0000000 6.00 2.125 2.3333333 4.50 1.4285714 3.652174  5.15 4.777778
53  3.8333333 7.50 3.250 0.6666667 1.50 0.1428571 3.260870  4.20 5.388889
54  5.1666667 4.75 1.750 3.3333333 3.50 2.1428571 3.478261  5.10 4.194444
55  5.0000000 6.00 4.125 2.0000000 4.50 2.1428571 4.173913  4.80 4.416667
56  5.6666667 5.25 2.125 2.6666667 4.50 1.4285714 4.043478  5.70 5.555556
57  4.0000000 4.75 3.250 1.6666667 3.00 1.7142857 4.173913  5.25 5.638889
58  5.8333333 5.25 3.875 2.6666667 5.50 2.4285714 3.434783  5.80 4.750000
59  3.8333333 6.50 2.000 1.0000000 2.50 1.2857143 3.391304  4.55 4.833333
60  4.1666667 5.75 1.750 1.3333333 3.00 1.0000000 3.521739  6.15 3.944444
61  5.3333333 5.75 3.375 4.0000000 5.50 2.5714286 5.260870  6.20 6.138889
62  4.0000000 6.00 3.625 2.6666667 5.00 2.1428571 4.913043  5.35 4.777778
63  5.3333333 6.75 1.375 1.6666667 2.50 0.7142857 2.652174  3.85 5.333333
64  5.3333333 5.00 1.250 3.3333333 4.50 2.4285714 5.521739  5.45 5.833333
65  3.6666667 5.75 3.625 2.6666667 5.00 1.4285714 3.304348  4.50 5.027778
66  6.5000000 6.00 2.500 3.6666667 4.50 3.0000000 5.695652  5.40 6.305556
67  4.0000000 9.25 4.000 3.0000000 3.75 1.0000000 3.608696  5.75 6.194444
68  4.6666667 5.75 3.625 2.6666667 2.75 1.8571429 3.869565  6.10 4.250000
69  2.8333333 5.00 0.875 3.0000000 2.50 1.5714286 6.826087  5.70 3.916667
70  4.6666667 5.00 0.750 4.3333333 4.00 2.7142857 4.739130  5.95 5.416667
71  0.6666667 4.50 0.750 2.0000000 3.00 1.0000000 3.608696  5.50 4.611111
72  4.3333333 9.25 3.375 1.3333333 2.25 1.1428571 3.739130  5.75 5.166667
73  5.0000000 4.50 2.625 1.6666667 2.75 1.5714286 5.260870  7.35 5.972222
74  5.0000000 6.00 3.250 3.3333333 5.75 2.7142857 3.478261  4.60 4.277778
75  4.1666667 5.25 2.125 3.0000000 3.25 0.7142857 3.478261  5.70 4.583333
76  3.8333333 5.25 2.375 1.6666667 2.25 1.0000000 3.130435  5.75 4.666667
77  5.6666667 7.00 2.125 4.0000000 4.75 2.2857143 5.652174  5.70 6.472222
78  1.6666667 5.75 1.375 0.0000000 1.50 1.1428571 5.695652  4.00 5.638889
79  6.3333333 5.50 4.125 4.0000000 3.25 1.8571429 5.565217  7.35 5.750000
80  4.0000000 5.25 2.500 1.0000000 1.50 1.8571429 4.695652  5.15 5.333333
81  4.5000000 6.00 1.750 1.6666667 2.00 1.5714286 5.869565  6.45 6.027778
82  4.6666667 8.00 4.250 3.0000000 4.75 2.4285714 5.130435  5.75 5.305556
83  4.8333333 8.25 2.250 0.3333333 2.25 0.5714286 5.347826  5.20 5.777778
84  4.8333333 6.50 1.750 2.3333333 3.50 0.7142857 5.782609  5.75 4.972222
85  3.0000000 5.25 0.625 1.0000000 1.50 1.0000000 4.956522  4.85 4.583333
86  6.3333333 6.25 2.500 3.3333333 5.75 3.7142857 4.608696  6.85 5.472222
87  5.5000000 7.00 2.875 2.6666667 4.00 0.8571429 4.086957  5.40 5.972222
88  5.3333333 6.00 2.750 4.0000000 5.00 2.1428571 5.521739  4.55 5.138889
89  3.3333333 7.25 3.250 1.3333333 1.25 0.5714286 3.565217  5.55 4.888889
90  5.5000000 5.00 2.250 3.6666667 4.50 1.7142857 4.173913  5.95 6.666667
91  4.0000000 6.50 2.625 2.6666667 2.75 1.8571429 6.652174  7.50 5.444444
92  3.8333333 6.25 3.375 2.0000000 3.75 0.4285714 5.391304  7.80 6.111111
93  5.1666667 4.75 2.125 1.0000000 2.00 1.5714286 3.565217  6.40 5.722222
94  4.3333333 3.50 0.875 2.6666667 3.25 1.1428571 3.608696  4.80 3.944444
95  4.1666667 5.50 0.875 4.6666667 4.75 2.1428571 4.130435  5.35 3.777778
96  5.0000000 5.75 4.250 4.0000000 4.25 2.4285714 3.565217  3.60 5.444444
97  4.3333333 4.75 0.500 3.3333333 5.75 2.8571429 5.521739  5.85 4.222222
98  7.5000000 7.50 2.125 5.3333333 6.75 2.7142857 5.130435  5.55 7.222222
99  5.0000000 3.75 1.375 1.6666667 2.75 1.4285714 5.565217  6.10 4.611111
100 5.5000000 6.25 2.750 3.6666667 4.50 3.2857143 4.913043  5.15 5.750000
101 6.1666667 3.75 4.375 2.6666667 3.25 1.0000000 4.260870  5.30 6.250000
102 6.5000000 6.50 1.875 3.6666667 3.75 1.7142857 4.521739  4.40 6.583333
103 4.3333333 7.75 1.875 4.3333333 4.50 1.8571429 5.869565  6.25 4.111111
104 4.6666667 5.25 1.625 3.3333333 4.50 2.8571429 3.826087  4.85 4.833333
105 6.8333333 9.00 4.375 2.6666667 5.75 5.0000000 3.000000  6.20 6.027778
106 4.5000000 6.50 3.125 2.6666667 5.50 2.1428571 6.347826  6.50 6.166667
107 6.8333333 6.50 0.750 5.3333333 5.75 3.1428571 7.434783  5.70 5.194444
108 5.5000000 6.75 3.750 2.6666667 2.75 2.0000000 4.173913  6.80 7.000000
109 6.3333333 5.50 1.625 5.6666667 5.50 4.0000000 5.695652  6.40 7.527778
110 4.1666667 4.25 2.250 3.3333333 3.50 1.4285714 4.478261  4.15 3.361111
111 5.6666667 9.25 4.375 3.6666667 4.75 3.0000000 3.260870  3.90 4.861111
112 6.3333333 5.25 2.625 4.3333333 4.25 1.5714286 2.869565  6.00 5.444444
113 6.1666667 6.25 4.250 5.0000000 3.50 1.8571429 4.565217  5.35 5.444444
114 5.1666667 6.50 1.750 4.0000000 5.25 1.8571429 4.695652  4.30 6.000000
115 4.1666667 7.50 3.875 1.0000000 2.00 0.8571429 3.565217  3.95 6.333333
116 4.1666667 5.75 1.000 3.6666667 4.75 1.8571429 4.826087  5.85 5.416667
117 5.1666667 5.75 2.500 2.6666667 4.75 2.7142857 5.304348  6.85 5.000000
118 4.3333333 5.25 4.000 3.0000000 2.75 1.0000000 5.565217  6.60 6.444444
119 3.5000000 7.25 3.375 2.3333333 2.25 1.1428571 3.695652  5.20 5.166667
120 4.6666667 4.75 1.625 2.6666667 3.25 2.0000000 4.086957  5.55 5.444444
121 4.8333333 6.00 1.500 2.6666667 4.25 1.2857143 5.826087  6.40 6.861111
122 5.0000000 5.50 2.375 3.3333333 3.00 2.0000000 4.608696  6.40 3.305556
123 7.5000000 7.00 4.250 4.0000000 6.50 3.5714286 3.739130  7.60 6.500000
124 5.3333333 5.25 4.125 2.3333333 3.75 1.5714286 3.652174  5.35 4.777778
125 6.1666667 6.75 4.125 3.3333333 5.00 2.8571429 5.000000  6.90 6.388889
126 5.3333333 8.75 3.125 4.3333333 4.00 1.1428571 4.000000  6.95 5.666667
127 4.3333333 7.00 1.000 4.3333333 5.50 3.0000000 5.130435  5.65 4.916667
128 4.8333333 7.00 1.125 3.3333333 5.00 2.2857143 4.913043  5.25 4.972222
129 6.0000000 7.50 3.250 3.0000000 6.00 2.4285714 3.304348  5.10 5.777778
130 5.0000000 4.50 1.625 2.0000000 2.75 0.5714286 4.521739  6.25 6.305556
131 4.1666667 8.00 4.375 4.3333333 6.25 3.1428571 4.565217  7.80 7.000000
132 5.6666667 5.50 3.500 4.0000000 4.25 2.2857143 7.260870  6.35 7.194444
133 5.0000000 6.25 1.750 1.3333333 2.00 0.5714286 4.913043  6.00 4.805556
134 3.5000000 5.25 2.250 2.0000000 3.75 1.8571429 4.695652  5.25 3.722222
135 7.3333333 8.00 2.625 2.6666667 5.00 2.4285714 3.434783  6.00 6.166667
136 6.1666667 7.50 3.625 4.0000000 6.25 2.5714286 4.391304  5.85 6.111111
137 4.8333333 5.25 4.500 4.3333333 5.75 2.8571429 3.347826  5.10 5.444444
138 5.6666667 6.25 2.750 3.3333333 5.25 2.1428571 4.434783  5.15 4.555556
139 5.5000000 6.75 4.500 2.3333333 4.50 2.5714286 6.260870  6.55 6.888889
140 5.1666667 5.00 2.500 3.3333333 5.75 3.0000000 6.434783  8.30 7.083333
141 3.1666667 3.75 1.500 3.3333333 5.75 2.2857143 6.043478  5.25 6.222222
142 5.0000000 7.50 4.500 2.0000000 1.50 0.4285714 3.304348  5.25 5.722222
143 7.1666667 9.00 3.875 3.6666667 5.25 4.7142857 3.826087  7.10 5.777778
144 7.3333333 6.75 4.000 6.0000000 6.50 6.1428571 5.347826  5.75 6.611111
145 2.0000000 5.50 0.625 2.6666667 5.00 0.8571429 2.391304  5.60 5.972222
146 3.8333333 5.50 1.875 3.6666667 5.00 3.0000000 5.608696  4.90 3.861111
147 4.1666667 6.00 1.250 4.0000000 5.00 2.5714286 6.956522  6.25 6.305556
148 4.6666667 5.75 1.625 0.6666667 2.50 1.4285714 4.521739  5.00 4.750000
149 5.8333333 4.75 2.625 5.6666667 5.50 4.1428571 3.565217  6.30 5.472222
150 6.6666667 5.50 4.375 2.6666667 3.75 1.0000000 5.130435  4.75 5.222222
151 6.8333333 7.25 4.250 5.0000000 5.75 4.8571429 5.652174  7.55 6.166667
152 5.3333333 4.75 2.625 3.0000000 5.50 2.1428571 5.782609  7.90 6.944444
153 4.8333333 5.00 2.375 3.0000000 4.25 2.4285714 5.391304  7.50 5.416667
154 5.1666667 6.00 2.375 1.3333333 2.25 1.1428571 5.391304  6.15 5.194444
155 6.3333333 6.75 1.125 3.0000000 2.50 1.4285714 4.695652  5.60 4.138889
156 4.8333333 5.75 1.250 3.0000000 4.75 2.1428571 3.608696  6.15 4.694444
157 3.8333333 4.75 0.500 3.3333333 4.25 1.4285714 3.000000  4.10 4.333333
158 5.5000000 5.50 2.125 2.6666667 4.25 1.4285714 2.826087  4.90 5.416667
159 5.6666667 6.00 2.750 3.6666667 4.75 2.7142857 2.173913  4.30 6.333333
160 4.8333333 5.75 1.125 3.0000000 4.75 1.5714286 4.956522  5.15 4.000000
161 2.6666667 6.25 1.250 2.6666667 6.25 3.4285714 4.869565  6.10 4.444444
162 5.0000000 6.25 2.500 3.3333333 5.75 2.5714286 4.086957  5.65 5.583333
163 6.0000000 8.25 4.500 5.6666667 6.25 5.8571429 5.608696  6.95 9.250000
164 4.6666667 6.25 1.125 3.3333333 4.50 1.5714286 4.173913  4.75 4.833333
165 5.0000000 6.25 1.375 3.6666667 5.25 1.1428571 4.478261  5.70 5.472222
166 3.3333333 6.25 0.750 3.0000000 5.25 2.2857143 3.869565  5.05 4.944444
167 4.5000000 6.50 0.750 3.3333333 4.00 1.8571429 3.826087  5.35 3.805556
168 5.3333333 5.25 1.000 0.3333333 1.75 1.5714286 4.478261  6.80 4.277778
169 6.3333333 7.75 1.500 3.3333333 2.75 2.0000000 3.608696  5.40 5.583333
170 2.8333333 5.25 0.750 1.6666667 2.50 1.4285714 4.304348  4.35 4.083333
171 5.6666667 7.00 3.000 4.6666667 5.50 3.7142857 2.130435  4.20 4.750000
172 4.1666667 7.75 2.250 2.3333333 3.00 1.5714286 2.826087  4.00 3.472222
173 5.5000000 7.75 3.750 3.6666667 5.75 2.5714286 2.869565  5.65 5.166667
174 6.6666667 5.75 2.500 3.3333333 5.75 5.0000000 3.130435  5.20 6.166667
175 5.0000000 5.50 2.500 2.6666667 4.25 2.8571429 4.130435  5.50 4.472222
176 6.0000000 7.00 2.750 4.3333333 6.00 5.1428571 3.565217  5.15 5.694444
177 4.0000000 4.00 1.750 1.6666667 4.25 1.5714286 4.608696  4.00 3.277778
178 6.6666667 6.50 3.250 4.3333333 6.00 3.5714286 3.826087  6.05 6.250000
179 5.0000000 6.00 2.375 4.6666667 6.50 3.4285714 4.869565  5.75 5.138889
180 7.0000000 6.75 3.375 2.3333333 3.25 0.5714286 5.478261 10.00 6.555556
181 5.5000000 6.75 2.000 2.6666667 4.25 1.8571429 2.956522  5.80 6.083333
182 5.3333333 5.50 1.875 3.0000000 5.00 2.4285714 3.217391  5.90 5.305556
183 5.1666667 5.25 0.625 4.3333333 4.75 2.7142857 4.173913  6.05 5.000000
184 4.5000000 5.75 0.500 3.0000000 2.75 1.0000000 2.391304  5.90 4.944444
185 5.1666667 5.75 1.375 1.3333333 4.00 2.2857143 3.521739  6.40 5.250000
186 5.1666667 8.50 0.375 3.0000000 4.75 2.4285714 2.217391  4.60 5.055556
187 2.8333333 7.50 1.625 3.0000000 4.25 1.8571429 2.260870  4.70 3.972222
188 5.0000000 5.00 1.625 3.0000000 5.00 3.0000000 4.695652  5.65 5.000000
189 4.6666667 4.75 1.000 3.0000000 3.50 2.8571429 3.521739  5.80 4.416667
190 3.1666667 5.50 1.000 1.6666667 3.00 0.5714286 2.173913  3.60 4.500000
191 4.6666667 5.00 1.750 2.6666667 3.75 1.1428571 4.347826  7.20 5.694444
192 6.3333333 6.25 1.625 3.0000000 5.75 2.1428571 2.434783  6.50 5.500000
193 5.6666667 7.00 1.250 3.0000000 5.50 2.8571429 3.521739  4.80 5.527778
194 3.0000000 5.50 0.625 0.6666667 1.00 0.2857143 1.304348  3.05 3.111111
195 2.6666667 5.00 1.000 2.0000000 4.50 1.8571429 4.043478  5.55 5.138889
196 3.0000000 7.50 2.125 6.3333333 6.00 4.7142857 4.000000  5.40 4.111111
197 5.3333333 5.25 1.125 5.0000000 5.00 3.5714286 4.130435  4.60 4.944444
198 5.6666667 5.25 1.125 3.0000000 5.75 3.4285714 2.521739  4.55 4.805556
199 3.5000000 5.50 1.000 3.0000000 3.00 1.1428571 2.826087  4.20 3.805556
200 4.6666667 5.00 1.750 2.6666667 4.50 1.4285714 2.652174  5.00 5.944444
201 6.5000000 6.00 3.125 4.6666667 4.25 1.5714286 3.565217  5.20 6.777778
202 5.3333333 6.50 1.250 3.6666667 5.75 3.2857143 2.695652  4.15 3.972222
203 4.6666667 6.00 1.000 2.0000000 2.50 1.4285714 2.652174  3.85 4.416667
204 4.0000000 5.00 2.750 3.6666667 4.75 2.5714286 3.086956  4.70 4.277778
205 5.1666667 4.75 1.625 2.6666667 5.50 2.4285714 2.652174  3.50 3.333333
206 4.8333333 6.50 3.125 3.3333333 5.50 2.5714286 3.695652  6.40 5.611111
207 4.8333333 7.50 1.875 2.3333333 4.00 1.7142857 2.782609  3.70 4.583333
208 2.6666667 5.75 0.625 2.3333333 5.00 1.8571429 4.130435  6.40 3.805556
209 4.1666667 6.25 3.250 1.6666667 1.75 0.7142857 2.000000  3.60 3.361111
210 4.1666667 6.75 1.875 2.0000000 4.00 1.8571429 3.739130  4.95 5.944444
211 6.1666667 7.75 2.000 4.6666667 7.00 2.8571429 3.826087  5.70 6.194444
212 5.0000000 6.50 2.375 2.3333333 5.25 2.0000000 3.869565  6.05 6.166667
213 4.8333333 6.00 1.250 3.0000000 5.25 2.4285714 4.347826  6.05 5.722222
214 6.1666667 8.50 2.125 6.0000000 6.00 3.8571429 3.478261  5.60 6.361111
215 4.6666667 5.00 0.500 2.6666667 4.50 0.8571429 3.173913  4.20 4.472222
216 3.6666667 8.50 2.375 3.3333333 4.25 2.1428571 3.478261  5.05 5.138889
217 6.3333333 5.25 2.250 6.0000000 6.50 4.4285714 5.217391  5.45 5.694444
218 4.8333333 5.25 2.250 2.3333333 4.00 1.0000000 2.913044  4.40 5.000000
219 4.1666667 6.25 3.750 3.0000000 4.00 2.2857143 4.608696  6.35 4.861111
220 4.6666667 6.75 1.625 1.6666667 1.50 0.5714286 4.391304  5.30 4.777778
221 3.8333333 5.00 2.250 2.0000000 3.00 0.8571429 1.869565  5.00 4.027778
222 1.8333333 5.00 1.125 4.3333333 4.00 2.1428571 3.173913  3.65 3.611111
223 7.5000000 9.25 3.625 3.3333333 4.00 2.2857143 3.173913  5.05 6.111111
224 3.1666667 3.75 0.875 5.0000000 4.75 2.7142857 4.391304  5.60 3.222222
225 6.8333333 5.25 1.375 3.0000000 4.00 2.0000000 5.391304  5.95 6.111111
226 5.8333333 7.50 4.125 2.6666667 3.75 1.7142857 3.913044  6.30 6.722222
227 5.6666667 6.00 2.000 3.0000000 3.50 1.4285714 3.869565  5.80 6.555556
228 3.1666667 5.50 1.500 3.3333333 4.00 2.4285714 5.130435  5.80 7.000000
229 4.1666667 5.25 1.875 2.6666667 4.25 2.4285714 2.956522  4.10 4.722222
230 6.0000000 7.00 4.125 5.0000000 6.00 5.5714286 3.521739  5.20 5.833333
231 3.1666667 5.75 0.750 2.3333333 3.25 2.7142857 2.956522  4.85 4.138889
232 4.5000000 5.25 0.875 3.0000000 4.75 3.1428571 3.826087  5.45 5.305556
233 3.5000000 2.25 1.750 5.0000000 6.25 4.2857143 2.956522  4.55 5.027778
234 4.5000000 5.50 4.000 2.6666667 3.25 1.8571429 2.434783  3.75 4.916667
235 4.3333333 5.50 1.000 1.6666667 2.75 1.5714286 3.304348  5.70 4.333333
236 3.3333333 6.75 1.875 3.0000000 4.75 1.5714286 5.652174  5.25 6.694444
237 5.5000000 5.50 1.750 4.3333333 6.25 3.7142857 3.652174  6.35 6.388889
238 6.3333333 6.50 0.875 5.3333333 6.25 2.1428571 4.739130  5.55 5.555556
239 5.5000000 6.25 4.250 5.0000000 5.75 3.0000000 4.739130  7.15 6.833333
240 6.0000000 7.25 2.000 3.0000000 5.75 3.2857143 6.478261  7.80 6.861111
241 6.3333333 7.25 4.375 4.3333333 5.50 2.4285714 4.869565  6.30 6.305556
242 5.1666667 6.25 3.500 3.3333333 6.25 3.8571429 4.478261  4.90 4.666667
243 6.0000000 7.00 1.625 3.6666667 5.75 2.5714286 5.826087  5.10 6.222222
244 5.1666667 6.50 3.625 3.6666667 6.50 1.7142857 3.695652  4.95 5.250000
245 4.6666667 6.00 0.750 2.6666667 4.25 2.1428571 4.000000  4.75 5.972222
246 6.3333333 9.25 3.500 3.3333333 4.75 2.7142857 4.782609  6.75 5.527778
247 4.8333333 8.50 2.750 3.3333333 5.00 2.7142857 3.652174  6.60 4.083333
248 5.8333333 6.00 1.875 4.0000000 5.00 3.2857143 4.000000  6.25 6.444444
249 5.8333333 6.25 3.125 3.6666667 6.00 2.7142857 5.739130  5.90 5.194444
250 3.8333333 5.25 0.375 3.0000000 5.25 1.7142857 5.782609  5.55 6.527778
251 5.1666667 7.00 3.125 4.0000000 5.75 2.1428571 6.043478  5.50 5.527778
252 8.5000000 6.50 4.250 6.0000000 6.50 5.4285714 5.782609  8.05 6.916667
253 5.5000000 7.00 2.250 5.0000000 5.75 4.2857143 4.173913  6.10 5.888889
254 3.5000000 6.50 2.125 4.0000000 4.00 4.7142857 5.130435  5.60 3.666667
255 6.1666667 8.00 1.375 5.3333333 6.25 4.5714286 3.478261  4.75 4.750000
256 6.1666667 5.00 2.625 2.6666667 4.75 2.7142857 3.391304  5.05 4.972222
257 6.1666667 6.50 3.625 2.3333333 4.75 2.7142857 4.434783  5.65 5.833333
258 6.5000000 6.50 3.000 5.6666667 6.50 4.2857143 5.000000  6.80 7.388889
259 6.5000000 8.50 4.125 3.3333333 4.75 2.2857143 4.130435  5.95 6.666667
260 3.0000000 4.00 0.500 3.3333333 5.25 2.7142857 2.956522  4.00 3.472222
261 4.6666667 7.00 2.250 3.3333333 6.25 3.2857143 3.782609  4.65 4.722222
262 4.8333333 5.75 1.125 2.0000000 6.00 1.2857143 4.347826  9.10 6.305556
263 4.1666667 5.00 1.375 4.0000000 4.75 2.4285714 4.826087  6.25 5.722222
264 4.8333333 8.25 1.375 4.3333333 5.75 3.7142857 3.391304  5.15 5.500000
265 5.3333333 5.25 1.875 4.6666667 5.00 4.1428571 5.086957  5.40 6.583333
266 5.0000000 5.25 2.625 4.0000000 6.00 3.8571429 4.173913  5.65 5.722222
267 5.8333333 7.00 2.375 6.0000000 6.75 4.7142857 6.304348  5.85 6.277778
268 2.6666667 8.50 4.000 4.6666667 5.50 3.4285714 4.304348  5.40 5.972222
269 6.3333333 7.50 4.125 4.6666667 5.50 4.1428571 4.217391  6.00 7.000000
270 4.3333333 6.25 0.875 3.3333333 3.75 1.8571429 5.000000  6.45 5.083333
271 5.5000000 7.00 1.500 4.6666667 5.25 2.0000000 4.608696  5.30 5.194444
272 4.6666667 6.75 4.000 3.3333333 4.75 2.4285714 3.000000  6.50 5.500000
273 4.5000000 5.50 2.000 2.6666667 4.75 2.1428571 5.869565  7.10 6.250000
274 4.3333333 7.25 1.250 2.6666667 4.75 1.8571429 3.695652  5.45 6.111111
275 5.0000000 8.00 3.250 2.3333333 3.75 1.4285714 4.739130  7.00 4.944444
276 3.6666667 5.75 1.000 1.3333333 3.25 2.4285714 4.000000  4.05 4.166667
277 5.3333333 6.00 1.000 3.0000000 4.75 2.0000000 3.391304  4.45 5.305556
278 5.1666667 5.75 1.625 4.0000000 4.75 2.1428571 3.565217  5.40 5.055556
279 5.3333333 6.25 2.125 1.6666667 2.75 2.2857143 4.608696  7.15 5.555556
280 6.6666667 5.25 1.875 2.6666667 4.50 1.8571429 3.652174  6.30 5.972222
281 3.5000000 5.00 1.375 2.3333333 4.75 1.1428571 3.478261  5.85 6.527778
282 5.1666667 6.25 3.250 5.0000000 6.00 3.2857143 2.391304  4.00 4.277778
283 4.0000000 6.00 2.000 2.6666667 4.50 1.7142857 3.347826  4.70 3.750000
284 6.8333333 6.25 1.875 4.0000000 5.25 2.7142857 2.782609  4.70 5.250000
285 5.0000000 5.75 1.250 2.6666667 3.50 1.7142857 4.260870  6.60 6.666667
286 5.8333333 5.00 2.000 3.3333333 4.00 1.4285714 3.478261  5.25 5.694444
287 5.6666667 6.75 2.125 4.3333333 6.25 4.5714286 3.347826  5.95 5.416667
288 4.1666667 5.50 1.625 2.3333333 4.00 1.5714286 5.869565  6.30 5.666667
289 4.0000000 6.25 0.750 3.0000000 4.75 3.2857143 5.217391  6.55 5.722222
290 6.0000000 6.50 2.125 3.3333333 4.75 1.4285714 2.434783  5.75 5.000000
291 3.3333333 4.75 0.875 2.6666667 5.00 0.8571429 5.826087  5.50 6.777778
292 4.6666667 6.00 1.125 1.3333333 3.00 1.5714286 4.173913  5.75 4.833333
293 5.6666667 5.50 1.625 3.3333333 4.25 2.0000000 5.478261  6.00 4.500000
294 5.6666667 5.25 2.375 3.3333333 4.50 2.0000000 3.391304  5.15 6.333333
295 5.8333333 7.00 1.250 3.0000000 3.25 1.5714286 4.173913  4.85 5.777778
296 6.1666667 6.50 3.000 3.0000000 4.25 2.8571429 3.043478  4.25 5.666667
297 4.0000000 7.00 1.375 2.6666667 4.25 1.0000000 5.086957  5.60 5.250000
298 3.0000000 6.00 1.625 2.3333333 4.00 1.0000000 4.608696  6.05 6.083333
299 4.6666667 5.50 1.875 3.6666667 5.75 4.2857143 4.000000  6.00 7.611111
300 4.3333333 6.75 0.500 3.6666667 4.50 2.0000000 5.086957  6.20 4.388889
301 4.3333333 6.00 3.375 3.6666667 5.75 3.1428571 4.086957  6.95 5.166667
    ageyr agemo      school
1      13     1     Pasteur
2      13     7     Pasteur
3      13     1     Pasteur
4      13     2     Pasteur
5      12     2     Pasteur
6      14     1     Pasteur
7      12     1     Pasteur
8      12     2     Pasteur
9      13     0     Pasteur
10     12     5     Pasteur
11     12     2     Pasteur
12     12    11     Pasteur
13     12     7     Pasteur
14     12     8     Pasteur
15     12     6     Pasteur
16     12     1     Pasteur
17     14    11     Pasteur
18     13     5     Pasteur
19     12     8     Pasteur
20     12     3     Pasteur
21     14    10     Pasteur
22     12     9     Pasteur
23     12    11     Pasteur
24     12     8     Pasteur
25     12     3     Pasteur
26     12     7     Pasteur
27     12     8     Pasteur
28     13     2     Pasteur
29     12     5     Pasteur
30     12     2     Pasteur
31     12     7     Pasteur
32     16     0     Pasteur
33     12     2     Pasteur
34     12     3     Pasteur
35     13     3     Pasteur
36     13    10     Pasteur
37     12     9     Pasteur
38     12     8     Pasteur
39     12     6     Pasteur
40     12     5     Pasteur
41     12     1     Pasteur
42     13     6     Pasteur
43     13     8     Pasteur
44     14     8     Pasteur
45     14     2     Pasteur
46     13     7     Pasteur
47     13     5     Pasteur
48     13     6     Pasteur
49     12    11     Pasteur
50     13     2     Pasteur
51     13     7     Pasteur
52     12     4     Pasteur
53     15     7     Pasteur
54     13     4     Pasteur
55     12     8     Pasteur
56     14     4     Pasteur
57     16     2     Pasteur
58     13     3     Pasteur
59     14     0     Pasteur
60     15     3     Pasteur
61     12     5     Pasteur
62     12     9     Pasteur
63     12    10     Pasteur
64     13    11     Pasteur
65     13     1     Pasteur
66     12    11     Pasteur
67     14    11     Pasteur
68     13     0     Pasteur
69     13     9     Pasteur
70     12     8     Pasteur
71     14     2     Pasteur
72     13     3     Pasteur
73     13     1     Pasteur
74     13     7     Pasteur
75     12     9     Pasteur
76     13     9     Pasteur
77     12     6     Pasteur
78     16     7     Pasteur
79     14     7     Pasteur
80     14     1     Pasteur
81     14    11     Pasteur
82     13     7     Pasteur
83     14     5     Pasteur
84     13     2     Pasteur
85     14     7     Pasteur
86     12    11     Pasteur
87     13     8     Pasteur
88     13     3     Pasteur
89     13    11     Pasteur
90     13    10     Pasteur
91     13    10     Pasteur
92     14     0     Pasteur
93     15     4     Pasteur
94     13     8     Pasteur
95     14    10     Pasteur
96     13     8     Pasteur
97     13     4     Pasteur
98     13     2     Pasteur
99     13    11     Pasteur
100    13     4     Pasteur
101    15     3     Pasteur
102    13    10     Pasteur
103    13     3     Pasteur
104    13     4     Pasteur
105    15     3     Pasteur
106    12    10     Pasteur
107    13     5     Pasteur
108    13    10     Pasteur
109    14     5     Pasteur
110    13     3     Pasteur
111    13     4     Pasteur
112    13     1     Pasteur
113    13     4     Pasteur
114    14     0     Pasteur
115    14     5     Pasteur
116    13     1     Pasteur
117    16     0     Pasteur
118    13    10     Pasteur
119    13     4     Pasteur
120    16     5     Pasteur
121    14     4     Pasteur
122    15     1     Pasteur
123    14     4     Pasteur
124    15     8     Pasteur
125    14     5     Pasteur
126    15     4     Pasteur
127    13    11     Pasteur
128    13    11     Pasteur
129    13     4     Pasteur
130    14     1     Pasteur
131    13    11     Pasteur
132    14     1     Pasteur
133    15     3     Pasteur
134    13     6     Pasteur
135    14     7     Pasteur
136    13     4     Pasteur
137    13    10     Pasteur
138    13     2     Pasteur
139    14     2     Pasteur
140    14     7     Pasteur
141    14     0     Pasteur
142    14     9     Pasteur
143    15     0     Pasteur
144    12     9     Pasteur
145    15     8     Pasteur
146    13     6     Pasteur
147    13     8     Pasteur
148    15     8     Pasteur
149    14     4     Pasteur
150    15     1     Pasteur
151    13    11     Pasteur
152    15     5     Pasteur
153    14     1     Pasteur
154    15     1     Pasteur
155    15     7     Pasteur
156    15     6     Pasteur
157    13     0 Grant-White
158    11    10 Grant-White
159    12     6 Grant-White
160    11    11 Grant-White
161    12     5 Grant-White
162    12     6 Grant-White
163    12     8 Grant-White
164    11    11 Grant-White
165    12     5 Grant-White
166    12     5 Grant-White
167    12     0 Grant-White
168    12    10 Grant-White
169    12     9 Grant-White
170    12     8 Grant-White
171    12     9 Grant-White
172    13     1 Grant-White
173    12     1 Grant-White
174    13     1 Grant-White
175    12     7 Grant-White
176    12     1 Grant-White
177    12     9 Grant-White
178    12     8 Grant-White
179    12     6 Grant-White
180    14     0 Grant-White
181    12     3 Grant-White
182    12     4 Grant-White
183    12     9 Grant-White
184    14     7 Grant-White
185    13     2 Grant-White
186    12     5 Grant-White
187    12     0 Grant-White
188    12     0 Grant-White
189    12     6 Grant-White
190    13     2 Grant-White
191    12     0 Grant-White
192    12    10 Grant-White
193    11    11 Grant-White
194    13     8 Grant-White
195    12     7 Grant-White
196    12     4 Grant-White
197    12     6 Grant-White
198    12     6 Grant-White
199    12     1 Grant-White
200    12     9 Grant-White
201    12     3 Grant-White
202    12     4 Grant-White
203    12    10 Grant-White
204    12     5 Grant-White
205    11    11 Grant-White
206    12     8 Grant-White
207    11    11 Grant-White
208    14     1 Grant-White
209    12     4 Grant-White
210    13     2 Grant-White
211    12     3 Grant-White
212    12    11 Grant-White
213    11     5 Grant-White
214    12     4 Grant-White
215    12     0 Grant-White
216    13    11 Grant-White
217    12     4 Grant-White
218    12     9 Grant-White
219    12    10 Grant-White
220    13     7 Grant-White
221    13     2 Grant-White
222    14     0 Grant-White
223    12    11 Grant-White
224    13     0 Grant-White
225    12     8 Grant-White
226    12     3 Grant-White
227    12     0 Grant-White
228    13     2 Grant-White
229    12     7 Grant-White
230    11     4 Grant-White
231    14     0 Grant-White
232    12     9 Grant-White
233    12     5 Grant-White
234    12     9 Grant-White
235    12     7 Grant-White
236    14     2 Grant-White
237    13     1 Grant-White
238    12    11 Grant-White
239    13     2 Grant-White
240    13     6 Grant-White
241    13     9 Grant-White
242    13     2 Grant-White
243    13     1 Grant-White
244    13     8 Grant-White
245    14     2 Grant-White
246    13     8 Grant-White
247    13     6 Grant-White
248    13     7 Grant-White
249    13     3 Grant-White
250    12    10 Grant-White
251    13     5 Grant-White
252    14     6 Grant-White
253    14     9 Grant-White
254    15    11 Grant-White
255    13     9 Grant-White
256    13     1 Grant-White
257    13     9 Grant-White
258    13     7 Grant-White
259    13     0 Grant-White
260    13     8 Grant-White
261    12     9 Grant-White
262    14    10 Grant-White
263    13     5 Grant-White
264    13     4 Grant-White
265    13     6 Grant-White
266    13     6 Grant-White
267    13     5 Grant-White
268    12     1 Grant-White
269    13     5 Grant-White
270    13     2 Grant-White
271    13     7 Grant-White
272    15     3 Grant-White
273    13     3 Grant-White
274    14     4 Grant-White
275    14     7 Grant-White
276    13     3 Grant-White
277    13     8 Grant-White
278    14     6 Grant-White
279    14     9 Grant-White
280    16     4 Grant-White
281    12    11 Grant-White
282    13     1 Grant-White
283    14     1 Grant-White
284    14     3 Grant-White
285    14     9 Grant-White
286    16     1 Grant-White
287    13     5 Grant-White
288    14     3 Grant-White
289    13     1 Grant-White
290    14     4 Grant-White
291    15     6 Grant-White
292    14    11 Grant-White
293    12    11 Grant-White
294    14     3 Grant-White
295    13     0 Grant-White
296    13     3 Grant-White
297    13     5 Grant-White
298    14    10 Grant-White
299    14     3 Grant-White
300    14     2 Grant-White
301    13     5 Grant-White

Not Important, But Helpful

Changing variable position in R is a pain:

head(HolzingerSwineford1939[, c(1, 7:15, 2:6)])
  id       x1   x2    x3       x4   x5        x6       x7   x8       x9
1  1 3.333333 7.75 0.375 2.333333 5.75 1.2857143 3.391304 5.75 6.361111
2  2 5.333333 5.25 2.125 1.666667 3.00 1.2857143 3.782609 6.25 7.916667
3  3 4.500000 5.25 1.875 1.000000 1.75 0.4285714 3.260870 3.90 4.416667
4  4 5.333333 7.75 3.000 2.666667 4.50 2.4285714 3.000000 5.30 4.861111
5  5 4.833333 4.75 0.875 2.666667 4.00 2.5714286 3.695652 6.30 5.916667
6  6 5.333333 5.00 2.250 1.000000 3.00 0.8571429 4.347826 6.65 7.500000
  sex ageyr agemo  school grade
1   1    13     1 Pasteur     7
2   2    13     7 Pasteur     7
3   2    13     1 Pasteur     7
4   1    13     2 Pasteur     7
5   2    12     2 Pasteur     7
6   2    14     1 Pasteur     7
HolzingerSwineford1939 %>% 
  select(id, starts_with("x"), everything()) %>% 
  head()
  id       x1   x2    x3       x4   x5        x6       x7   x8       x9
1  1 3.333333 7.75 0.375 2.333333 5.75 1.2857143 3.391304 5.75 6.361111
2  2 5.333333 5.25 2.125 1.666667 3.00 1.2857143 3.782609 6.25 7.916667
3  3 4.500000 5.25 1.875 1.000000 1.75 0.4285714 3.260870 3.90 4.416667
4  4 5.333333 7.75 3.000 2.666667 4.50 2.4285714 3.000000 5.30 4.861111
5  5 4.833333 4.75 0.875 2.666667 4.00 2.5714286 3.695652 6.30 5.916667
6  6 5.333333 5.00 2.250 1.000000 3.00 0.8571429 4.347826 6.65 7.500000
  sex ageyr agemo  school grade
1   1    13     1 Pasteur     7
2   2    13     7 Pasteur     7
3   2    13     1 Pasteur     7
4   1    13     2 Pasteur     7
5   2    12     2 Pasteur     7
6   2    14     1 Pasteur     7

Your Turn!

  1. Use that Stata test file.

  2. Grab every lvi, effect, leader, and cred variable

  3. Use summary to understand your data.

  4. Now, just keep every lvi variable.

  5. Use a corrplot to see relationships.

    • corrplot needs a correlation matrix (use cor)
install.packages("corrplot")

Subsetting/Filtering

One of the more frequent tasks is related to filtering/subsetting your data. You often want to impose some types of rules on your data (e.g., US only, date ranges).

Base

R gives us all the ability in the world to filter data.

summary(mtcars[mtcars$mpg < mean(mtcars$mpg), ])
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :6.000   Min.   :145.0   Min.   :105.0  
 1st Qu.:14.78   1st Qu.:8.000   1st Qu.:275.8   1st Qu.:156.2  
 Median :15.65   Median :8.000   Median :311.0   Median :180.0  
 Mean   :15.90   Mean   :7.556   Mean   :313.8   Mean   :191.9  
 3rd Qu.:18.02   3rd Qu.:8.000   3rd Qu.:360.0   3rd Qu.:226.2  
 Max.   :19.70   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :2.770   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.070   1st Qu.:3.440   1st Qu.:16.10   1st Qu.:0.0000  
 Median :3.150   Median :3.570   Median :17.35   Median :0.0000  
 Mean   :3.302   Mean   :3.839   Mean   :17.10   Mean   :0.1667  
 3rd Qu.:3.600   3rd Qu.:3.844   3rd Qu.:17.94   3rd Qu.:0.0000  
 Max.   :4.220   Max.   :5.424   Max.   :20.22   Max.   :1.0000  
       am              gear            carb       amFactor
 Min.   :0.0000   Min.   :3.000   Min.   :1.000   0:15    
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.250   1: 3    
 Median :0.0000   Median :3.000   Median :4.000           
 Mean   :0.1667   Mean   :3.444   Mean   :3.556           
 3rd Qu.:0.0000   3rd Qu.:3.750   3rd Qu.:4.000           
 Max.   :1.0000   Max.   :5.000   Max.   :8.000           

Unless you know exactly what you are doing, this is a bit hard to read – you might be asking yourself what the comma means and why there is nothing after it.

dplyr

When we use filter, we are specifying what it is that we want to keep.

Filter this or that:

mtcars %>% 
  filter(cyl == 4 | cyl == 8) %>% 
  summary()
      mpg             cyl            disp             hp       
 Min.   :10.40   Min.   :4.00   Min.   : 71.1   Min.   : 52.0  
 1st Qu.:15.20   1st Qu.:4.00   1st Qu.:120.1   1st Qu.: 93.0  
 Median :18.70   Median :8.00   Median :275.8   Median :150.0  
 Mean   :20.19   Mean   :6.24   Mean   :244.0   Mean   :153.5  
 3rd Qu.:24.40   3rd Qu.:8.00   3rd Qu.:351.0   3rd Qu.:205.0  
 Max.   :33.90   Max.   :8.00   Max.   :472.0   Max.   :335.0  
      drat            wt             qsec             vs     
 Min.   :2.76   Min.   :1.513   Min.   :14.50   Min.   :0.0  
 1st Qu.:3.08   1st Qu.:2.320   1st Qu.:16.90   1st Qu.:0.0  
 Median :3.69   Median :3.435   Median :17.60   Median :0.0  
 Mean   :3.60   Mean   :3.245   Mean   :17.81   Mean   :0.4  
 3rd Qu.:4.08   3rd Qu.:3.780   3rd Qu.:18.61   3rd Qu.:1.0  
 Max.   :4.93   Max.   :5.424   Max.   :22.90   Max.   :1.0  
       am           gear           carb      amFactor
 Min.   :0.0   Min.   :3.00   Min.   :1.00   0:15    
 1st Qu.:0.0   1st Qu.:3.00   1st Qu.:2.00   1:10    
 Median :0.0   Median :3.00   Median :2.00           
 Mean   :0.4   Mean   :3.64   Mean   :2.64           
 3rd Qu.:1.0   3rd Qu.:4.00   3rd Qu.:4.00           
 Max.   :1.0   Max.   :5.00   Max.   :8.00           

Filter this and that:

mtcars %>% 
  filter(cyl == 4 & mpg > 25) %>% 
  summary()
      mpg             cyl         disp              hp        
 Min.   :26.00   Min.   :4   Min.   : 71.10   Min.   : 52.00  
 1st Qu.:28.07   1st Qu.:4   1st Qu.: 76.45   1st Qu.: 65.25  
 Median :30.40   Median :4   Median : 78.85   Median : 66.00  
 Mean   :30.07   Mean   :4   Mean   : 86.65   Mean   : 75.50  
 3rd Qu.:31.90   3rd Qu.:4   3rd Qu.: 91.08   3rd Qu.: 84.75  
 Max.   :33.90   Max.   :4   Max.   :120.30   Max.   :113.00  
      drat             wt             qsec             vs        
 Min.   :3.770   Min.   :1.513   Min.   :16.70   Min.   :0.0000  
 1st Qu.:4.080   1st Qu.:1.670   1st Qu.:17.30   1st Qu.:1.0000  
 Median :4.150   Median :1.885   Median :18.71   Median :1.0000  
 Mean   :4.252   Mean   :1.873   Mean   :18.40   Mean   :0.8333  
 3rd Qu.:4.378   3rd Qu.:2.089   3rd Qu.:19.33   3rd Qu.:1.0000  
 Max.   :4.930   Max.   :2.200   Max.   :19.90   Max.   :1.0000  
       am         gear            carb     amFactor
 Min.   :1   Min.   :4.000   Min.   :1.0   0:0     
 1st Qu.:1   1st Qu.:4.000   1st Qu.:1.0   1:6     
 Median :1   Median :4.000   Median :1.5           
 Mean   :1   Mean   :4.333   Mean   :1.5           
 3rd Qu.:1   3rd Qu.:4.750   3rd Qu.:2.0           
 Max.   :1   Max.   :5.000   Max.   :2.0           

Filter this out:

mtcars %>% 
  filter(cyl != 4) %>% 
  summary()
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :6.000   Min.   :145.0   Min.   :105.0  
 1st Qu.:15.00   1st Qu.:6.000   1st Qu.:225.0   1st Qu.:123.0  
 Median :16.40   Median :8.000   Median :301.0   Median :175.0  
 Mean   :16.65   Mean   :7.333   Mean   :296.5   Mean   :180.2  
 3rd Qu.:19.20   3rd Qu.:8.000   3rd Qu.:360.0   3rd Qu.:215.0  
 Max.   :21.40   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :2.620   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.070   1st Qu.:3.435   1st Qu.:16.46   1st Qu.:0.0000  
 Median :3.150   Median :3.520   Median :17.30   Median :0.0000  
 Mean   :3.348   Mean   :3.705   Mean   :17.17   Mean   :0.1905  
 3rd Qu.:3.730   3rd Qu.:3.840   3rd Qu.:17.98   3rd Qu.:0.0000  
 Max.   :4.220   Max.   :5.424   Max.   :20.22   Max.   :1.0000  
       am              gear            carb       amFactor
 Min.   :0.0000   Min.   :3.000   Min.   :1.000   0:16    
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000   1: 5    
 Median :0.0000   Median :3.000   Median :4.000           
 Mean   :0.2381   Mean   :3.476   Mean   :3.476           
 3rd Qu.:0.0000   3rd Qu.:4.000   3rd Qu.:4.000           
 Max.   :1.0000   Max.   :5.000   Max.   :8.000           

Naturally, it can also take a function

mtcars %>% 
  filter(mpg < mean(mpg)) %>% 
  summary()
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :6.000   Min.   :145.0   Min.   :105.0  
 1st Qu.:14.78   1st Qu.:8.000   1st Qu.:275.8   1st Qu.:156.2  
 Median :15.65   Median :8.000   Median :311.0   Median :180.0  
 Mean   :15.90   Mean   :7.556   Mean   :313.8   Mean   :191.9  
 3rd Qu.:18.02   3rd Qu.:8.000   3rd Qu.:360.0   3rd Qu.:226.2  
 Max.   :19.70   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :2.770   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.070   1st Qu.:3.440   1st Qu.:16.10   1st Qu.:0.0000  
 Median :3.150   Median :3.570   Median :17.35   Median :0.0000  
 Mean   :3.302   Mean   :3.839   Mean   :17.10   Mean   :0.1667  
 3rd Qu.:3.600   3rd Qu.:3.844   3rd Qu.:17.94   3rd Qu.:0.0000  
 Max.   :4.220   Max.   :5.424   Max.   :20.22   Max.   :1.0000  
       am              gear            carb       amFactor
 Min.   :0.0000   Min.   :3.000   Min.   :1.000   0:15    
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.250   1: 3    
 Median :0.0000   Median :3.000   Median :4.000           
 Mean   :0.1667   Mean   :3.444   Mean   :3.556           
 3rd Qu.:0.0000   3rd Qu.:3.750   3rd Qu.:4.000           
 Max.   :1.0000   Max.   :5.000   Max.   :8.000           

Your Turn

For now, we are going to stick with that stataExample data.

  1. Select the same variables, but also include Rater.

  2. Filter the data on Rater – check the values and filter both ways.

  3. Now check those correlations again!

  4. Throw the Gender variable in and filter on that.

New Variables and Recoding

Base

Adding a new variable in base R is as easy as the following:

mtcars$roundedMPG = round(mtcars$mpg)

dplyr

If, however, we want to do things in a tidy chunk, we need to use mutate.

mtcars = mtcars %>% 
  mutate(roundedMPG = round(mpg))

There is also transmute. Can anyone venture a guess as to what it might do?

Base Recoding

You will need to recode variables at some point. Depending on the nature of the recode it can be easy (e.g., to reverse code a scale, you just subtract every value by max value + 1).

You will need to do some more elaborate stuff:

mtcars$mpgLoHi = 0

mtcars$mpgLoHi[mtcars$mpg > median(mtcars$mpg)] = 1
mtcars$mpgLoHi = ifelse(mtcars$mpg > median(mtcars$mpg), 1, 0)

These are pretty good ways to do recoding of this nature, but what about this:

mtcars$vs[which(mtcars$vs == 0)] = "v"

mtcars$vs[which(mtcars$vs == 1)] = "s"

Or this:

mtcars$vs = ifelse(mtcars$vs == 0, "v", "s")

dplyr recoding

recode(mtcars$vs, `0` = "v", `1` = "s")

Communication

We won’t have any big end-of-day wrap exercises to do today. Instead, we are going to learn just a few cool things.

ggplot2

We already saw some ggplot2, but let’s take a few minutes to dive into a bit more.

Just like everything else in the tidyverse, ggplot2 provides a clear and consistent grammar, except the focus is on data visualization. With ggplot2, we can stack layer after layer into the plotting space to help visualize our data.

Let’s take a look at some good ggplot2 layering:

library(ggplot2)

library(lavaan)

testData = HolzingerSwineford1939

ggplot(testData, aes(x7, ageyr)) +
  geom_point()

Next, we can add some color:

ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75)

Now, we can add a smooth line:

ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75) + 
  geom_smooth()

And we can look at small multiples:

ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75) + 
  geom_smooth() +
  facet_grid(~ sex)

Let’s get those silly grey boxes out of there:

ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75) + 
  geom_smooth() +
  facet_grid(~ sex) +
  theme_minimal()

Perhaps add a better color scheme:

ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75) + 
  geom_smooth() +
  facet_grid(~ sex) +
  theme_minimal() +
  scale_color_brewer(palette = "Dark2")

We could keep going forever and tweak anything that you could imagine (labels, ticks, etc.), but this should give you a pretty good idea about what you can do with regard to static plots.

Oh…but we don’t have to stick with just static plots. We can use the plotly package to make our ggplot object interactive.

library(plotly)

radPlot = ggplot(testData, aes(x7, ageyr)) +
  geom_point(aes(color = as.factor(grade)), alpha = .75) + 
  geom_smooth() +
  facet_grid(~ sex) +
  theme_minimal() +
  scale_color_brewer(palette = "Dark2")

ggplotly(radPlot)

You can also build plots with plotly, but we will save that for another day in the future.

DT

Visualizations are great and they often tell a better story than tables. Sometimes, though, you want to give people a glimpse of the data. The DT package let’s you create interactive data tables (they are JS data tables).

You could give people the entire data to explore:

library(DT)

datatable(testData)

You can also use the DT package to tidy your summaries into a nice dataframe:

lm(x7 ~ ageyr + school, data = testData) %>% 
  broom::tidy() %>% 
  mutate_if(is.numeric, round, 4) %>% 
  datatable()

We don’t want to get too far ahead of ourselves here – we will see more places to use this tomorrow.

R Markdown & Knitr

Do you have a moment to hear the good word of Donald Knuth? If you want to work in a reproducible fashion base and knitr

Day 1 Thought Question